﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Support;

namespace MS.InformationManagement.Core.Actions.Informado
{
    public class AssignDocumentCommand : IBaseCommand
    {
        #region [Private Fields]

        private SPFile _item;
        private Guid _sourceLibraryId;
        private SPFolder _sourceFile;
        private SPList _targetLibrary;
        private string _name;
        private Dictionary<string, string> _viewers;
        private SPWeb _web; 
        
        #endregion

        #region [Public Properties]

        public SPFile Item { get { return _item; } }

        #endregion

        public AssignDocumentCommand(string name, Dictionary<string, string> viewers, SPFolder sourceFile, Guid sourceLibraryId, SPList targetLibrary, SPWeb web)
        {
            _name = name;
            _viewers = viewers;
            _sourceFile = sourceFile;
            _sourceLibraryId = sourceLibraryId;
            _targetLibrary = targetLibrary;
            _web = web;
        }

        public void Execute()
        {
            UTF8Encoding encoding = new System.Text.UTF8Encoding();
            byte[] content = encoding.GetBytes(string.Empty);
            _item = _targetLibrary.RootFolder.Files.Add(_name, content);
            _item.Item[SPBuiltInFieldId.ContentType] = GlobalConstants.CTTN_INFORMATION;
            _item.Item[SPBuiltInFieldId.Title] = _sourceFile.Name;
            _item.Item[SPBuiltInFieldId.URL] = SPContext.Current.Web.Url + _viewers[_sourceFile.Item[SPBuiltInFieldId.ContentType].ToString()] + "?strategy=view&ListId="
                + _sourceLibraryId + "&ID=" + _sourceFile.Item.ID.ToString() + "&sender=" + _web.CurrentUser.Name + "&IsInformed=1";
            _item.Item[CustomSiteColumns.FIELD_DOCSET_ID_RADICADO] = _sourceFile.Item[CustomSiteColumns.FIELD_DOCSET_ID_RADICADO].ToString();
            _item.Item[CustomSiteColumns.FIELD_DOCSET_KIND] = TermStoreHelper.GetTaxonomyFieldByTerm(GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_TIPOS, GlobalConstants.METADATA_TERM_TIPO_INFORMADO);
            _item.Item[CustomSiteColumns.FIELD_DOCUMENT_DOCUMENTAL_TYPE] = _sourceFile.Item[CustomSiteColumns.FIELD_DOCUMENT_DOCUMENTAL_TYPE];
            _item.Item[CustomSiteColumns.FIELD_DOCUMENT_ASUNTO] = _sourceFile.Item[CustomSiteColumns.FIELD_DOCUMENT_ASUNTO];
            _item.Item[CustomSiteColumns.FIELD_DOCSET_SENDERRECEPT] = _sourceFile.Item[CustomSiteColumns.FIELD_DOCSET_SENDERRECEPT];
            _item.Item[CustomSiteColumns.FIELD_DOCUMENT_MAXRESPONSEDATE] = _sourceFile.Item[CustomSiteColumns.FIELD_DOCUMENT_MAXRESPONSEDATE];
            _item.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS] = _sourceFile.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS];
            _item.Item.Update();
        }

        public void Undo()
        {
            if (_item != null) { _item.Delete(); }
        }

    }
}
